#----------------------------------------------------------------------
# Initialize environment and alias
#----------------------------------------------------------------------
alias ls='ls -h --si --time-style=long-iso --color'
alias ll='lld --color=never'
alias la='ls -lAh'
alias grep='grep --color=tty'
alias cat='bat'
alias tmux='tmux -2 -f ~/.local/etc/tmux/tmux.conf'
alias lld='lsd -l'
alias path='echo $PATH | tr -s ":" "\n"'
alias gz='_fb() { file=$(basename $1); tar zcf $file-$(date +%Y%m%d).tar.gz $file; }; _fb'
alias sshhosts="grep -w -i -E 'Host|HostName' ~/.ssh/config | sed 's/Host //' | sed 's/HostName //'"
alias ip='ip --color'
alias sslocal='_f() { pkill -f ss-rs; [ -z "$1" ] && echo "invalid ip"; daemonize $HOME/.local/usr/libexec/ss-rs -s $1:80 -l 127.0.0.1:996 -k uI9xS7cO4xB0mY5vD2tJ -m chacha20-ietf-poly1305 ; }; _f'
alias pc=proxychains4
alias k=kubectl
alias fvim='vim $(fzf --bind=tab:preview-down,btab:preview-up)'
alias rm-by-inode='fx() { find . -inum $1 -delete ; }; fx'
alias sshkey='curl https://github.com/fjrti.keys | tee -a ~/.ssh/authorized_keys'
alias open='/mnt/c/windows/explorer.exe'
alias c='/mnt/c/windows/system32/clip.exe'

export EDITOR=vim
export TERM=xterm-256color
export TERMINFO=/usr/share/terminfo
export HISTIGNORE="?:cd:pwd:ls:ll:bg:fg"
export HISTCONTROL=ignoreboth
export INPUTRC=~/.local/etc/inputrc
export WORKON_HOME=~/.virtualenvs
export RIPGREP_CONFIG_PATH=~/.local/etc/ripgreprc
export LC_ALL=en_US.UTF-8

# go env config
# export GOROOT="/usr/local/go"
# export GOPATH=$HOME/go
# export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
# export GO111MODULE="on"
# export GOPROXY=https://goproxy.cn,direct
# export GOSUMDB=off

shopt -s histappend
shopt -s checkwinsize
shopt -s nocaseglob;
shopt -s cdspell;

export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'
export PS1="[\[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;10m\]\u\[$(tput sgr0)\]@\[$(tput sgr0)\]\[\033[38;5;9m\]\h\[$(tput sgr0)\] \w]\n\\$ \[$(tput sgr0)\]"

# disable ^s and ^q
stty -ixon 2> /dev/null
umask 022

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ]; then
        export PATH="$HOME/.local/bin:$PATH"
fi

# setup for local rust
if [ -d "$HOME/.cargo/bin" ]; then
	export PATH="$HOME/.cargo/bin:$PATH"
fi

# setup for local go
if [ -d "$HOME/go/bin" ]; then
	export PATH="$HOME/go/bin:$PATH"
fi

#----------------------------------------------------------------------
# exit if not bash/zsh, or not in an interactive shell
#----------------------------------------------------------------------
[ -z "$BASH_VERSION" ] && [ -z "$ZSH_VERSION" ] && return
[[ $- != *i* ]] && return

#----------------------------------------------------------------------
# keymap
#----------------------------------------------------------------------
# default bash key binding
if [[ -n "$BASH_VERSION" ]]; then
	bind '"\eh":"\C-b"'
	bind '"\el":"\C-f"'
	bind '"\ej":"\C-n"'
	bind '"\ek":"\C-p"'
	bind '"\eH":"\eb"'
	bind '"\eL":"\ef"'
	bind '"\eJ":"\C-a"'
	bind '"\eK":"\C-e"'
	bind '"\e;":"ll\n"'
elif [[ -n "$ZSH_VERSION" ]]; then
	bindkey -s '\e;' 'll\n'
	bindkey -s '\eu' 'ranger_cd\n'
fi

#----------------------------------------------------------------------
# https://github.com/rupa/z
# https://github.com/skywind3000/z.lua
#----------------------------------------------------------------------
if [[ -z "$DISABLE_Z_PLUGIN" ]]; then
	if [[ ! -d "$HOME/.local/var/zlua" ]]; then
		mkdir -p -m 700 "$HOME/.local/var/zlua" 2> /dev/null
	fi

	export _Z_CMD=j
	export _Z_DATA="$HOME/.local/var/zlua/z.txt"

	export _ZL_CMD=j
	export _ZL_DATA="$HOME/.local/var/zlua/zlua.txt"
	export _ZL_ECHO=1
	export _ZL_USE_LFS=1

	INIT_LUA="$HOME/.local/bin/slua"
	if [[ -x "$INIT_LUA" ]] && [[ -f "$HOME/.local/etc/z.lua" ]]; then
		eval "$($INIT_LUA $HOME/.local/etc/z.lua --init bash once echo enhanced fzf)"
		alias ji='j -i'
		alias jb='j -b'
		alias jf='j -I'
		alias jh='j -I -t .'
		alias jbi='j -b -i'
		alias jbf='j -b -I'
		_ZL_ECHO=1
	else
		[[ -f "$HOME/.local/etc/z.sh" ]] && . "$HOME/.local/etc/z.sh"
		alias jj='j'
	fi
fi

alias jj='j -c'
alias jjc='jj -c'


#----------------------------------------------------------------------
# commacd.sh
#----------------------------------------------------------------------
COMMACD_CD="cd"
[[ -e "$HOME/.local/etc/commacd.sh" ]] && . "$HOME/.local/etc/commacd.sh"

#----------------------------------------------------------------------
# m.sh - bookmark
#----------------------------------------------------------------------
[[ -e "$HOME/.local/etc/m.sh" ]] && . "$HOME/.local/etc/m.sh"

#----------------------------------------------------------------------
# other interactive shell settings
#----------------------------------------------------------------------
export GCC_COLORS=1

#----------------------------------------------------------------------
# complete settings TODO
#----------------------------------------------------------------------
if [ -d "$HOME/.local/etc/bash_completion.d" ]; then
  for file in $HOME/.local/etc/bash_completion.d/*; do
    source $file
  done
fi

partial_path="$HOME/.local/etc/bash_complete_partial_path" 
[[ -e $partial_path ]] && . "$partial_path" && _bcpp --defaults

#----------------------------------------------------------------------
# fzf settings
#----------------------------------------------------------------------
source "$HOME/.local/etc/fzf-key-bindings.sh"


#----------------------------------------------------------------------
# ssh host completion 
#----------------------------------------------------------------------
complete -W "$(awk '/^Host/{if ($2!="*") print $2}' ~/.ssh/config)" ssh
for host in $(awk '/^Host/{if ($2!="*") print $2}' ~/.ssh/config); do
    alias $host="ssh $host"
done

#----------------------------------------------------------------------
# execute history merge if it exists
#----------------------------------------------------------------------
if [ -f "$HOME/.local/etc/sync-history.sh" ]; then
	source "$HOME/.local/etc/sync-history.sh"
fi

#----------------------------------------------------------------------
# kubectl context
#----------------------------------------------------------------------
# Get current context
alias krc='kubectl config current-context'
# List all contexts
alias klc='kubectl config get-contexts -o name | sed "s/^/  /;\|^  $(krc)$|s/ /*/"'
# Change current context
alias kcc='kubectl config use-context "$(klc | fzf -e | sed "s/^..//")"'
# Get current namespace
alias krn='kubectl config get-contexts --no-headers "$(krc)" | awk "{print \$5}" | sed "s/^$/default/"'
# List all namespaces
alias kln='kubectl get -o name ns | sed "s|^.*/|  |;\|^  $(krn)$|s/ /*/"'
# Change current namespace
alias kcn='kubectl config set-context --current --namespace "$(kln | fzf -e | sed "s/^..//")"'

#----------------------------------------------------------------------
# todo list
#----------------------------------------------------------------------
export TODO="${HOME}/.local/etc/todo.txt"
tadd() { [ $# -eq 0 ] && cat $TODO || (echo "$(echo $* | md5sum | cut -c 1-4) 👉🏼 $*" >> $TODO && cat $TODO) ;}
trm() { sed -i '' "/^$*/d" $TODO && cat $TODO ;}
tl() { cat $TODO ;}
tclear() { cat /dev/null > $TODO ;}

rename-spaces() { for f in *' '*; do mv "$f" "${f// /_}"; done; }
tmp() { local d=$(mktemp -d); cd "$d" && echo "Temp dir: $d"; }
randpw() { < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-16}; echo; }
f() { fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' "$@"; }
fe() { local file=$(f "$@"); [[ -n "$file" ]] && ${EDITOR:-vim} "$file"; }
# fd() { local dir=$(find . -type d -not -path '*/\.git/*' 2>/dev/null | fzf); [[ -n "$dir" ]] && cd "$dir"; }
pk() { ps aux | fzf --header='[kill]' | awk '{print $2}' | xargs -r kill -9; }
